/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 20:36:40
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-04-12 10:23:29
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/src/xict_ui_driver/utils/basecansdk.cpp
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#include "xict_ui_driver/utils/basecansdk.h"
#include "xict_ui_common/utils/config.h"

namespace xict_ui_driver {

    CanSdk::CanSdk(QObject* parent) : QObject(parent) {
        qRegisterMetaType<xcmg_proto::Grader_CAN>(
            "const xcmg_proto::Grader_CAN &");
        qRegisterMetaType<xcmg_proto::Loader_CAN>(
            "const xcmg_proto::Loader_CAN &");
        qRegisterMetaType<xcmg_proto::Scraper_CAN>(
            "const xcmg_proto::Scraper_CAN &");
    }

    void CanSdk::EnableSdk(int vehicle_type) {
        m_current_vehicle_type = vehicle_type;
        if (m_current_vehicle_type == 1) {
            m_grader_can_sub =
                eCAL::protobuf::CSubscriber<xcmg_proto::Grader_CAN>(
                    xict_ui_common::Config::GetInstance().GetYaml(
                        "cockpit", "vehicle_type"));

            m_grader_can_sub.AddReceiveCallback(
                std::bind(&CanSdk::GraderCanCallback, this,
                          std::placeholders::_1, std::placeholders::_2));
        } else if (m_current_vehicle_type == 2) {
            m_loader_can_sub =
                eCAL::protobuf::CSubscriber<xcmg_proto::Loader_CAN>(
                    xict_ui_common::Config::GetInstance().GetYaml(
                        "cockpit", "vehicle_type"));

            m_loader_can_sub.AddReceiveCallback(
                std::bind(&CanSdk::LoaderCanCallback, this,
                          std::placeholders::_1, std::placeholders::_2));
        } else if (m_current_vehicle_type == 3) {
            m_scraper_can_sub =
                eCAL::protobuf::CSubscriber<xcmg_proto::Scraper_CAN>(
                    xict_ui_common::Config::GetInstance().GetYaml(
                        "cockpit", "vehicle_type"));
            m_scraper_can_sub.AddReceiveCallback(
                std::bind(&CanSdk::ScraperCanCallback, this,
                          std::placeholders::_1, std::placeholders::_2));
        }
    }

    void CanSdk::DisableSdk() {
        if (m_current_vehicle_type == 1)
            m_grader_can_sub.Destroy();
        else if (m_current_vehicle_type == 2)
            m_loader_can_sub.Destroy();
        else if (m_current_vehicle_type == 3)
            m_scraper_can_sub.Destroy();
    }

    void CanSdk::GraderCanCallback(const char*,
                                   const xcmg_proto::Grader_CAN& msgs) {
        emit signalGraderCan(msgs);
    }

    void CanSdk::LoaderCanCallback(const char*,
                                   const xcmg_proto::Loader_CAN& msgs) {
        emit signalLoaderCan(msgs);
    }
    void CanSdk::ScraperCanCallback(const char*,
                                    const xcmg_proto::Scraper_CAN& msgs) {
        // qDebug() << "test ... " << "Scraper";
        emit signalScraperCan(msgs);
    }
}   // namespace xict_ui_driver
